package cms.simmytech.util;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

@Component
public class FileUtil {
	@Value("${simmytech.savePath}")
	private String savePath;

	@Value("${simmytech.reqPath}")
	private String reqPath;

	/**
	 * 第一步：判断文件是否为空 true：返回提示为空信息 false：执行第二步 第二步：判断目录是否存在 不存在：创建目录
	 * 第三部：通过输出流将文件写入硬盘文件夹并关闭流
	 * 
	 * @param file
	 * @return
	 */

	public String uploadFile(MultipartFile file) {
		String fullReqPath = "";
		// 获取文件后缀
		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".") + 1);
		// 源文件名
		String fileName = file.getOriginalFilename();
		// 新文件名称
		String fileNewName = UUID.randomUUID().toString() + "." + suffix;
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		String time = sf.format(new Date());
		String saveFullPath = savePath + time;
		File targetFile = new File(saveFullPath);
		// 第一步：判断文件是否为空
		if (file.isEmpty())
			return fileName + "文件为空";
		// 第二步：判断目录是否存在 不存在：创建目录
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		// 第三部：通过输出流将文件写入硬盘文件夹并关闭流
		BufferedOutputStream stream = null;
		try {
			stream = new BufferedOutputStream(new FileOutputStream(saveFullPath + "/" + fileNewName));
			stream.write(file.getBytes());
			stream.flush();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stream != null)
					stream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		fullReqPath = reqPath + time + "/" + fileNewName;
		return fullReqPath;
	}

	public String readFile(String filePath) {
		File file=new File(filePath);
		if(!file.exists()){
			return null;
		}
		StringBuffer sb = new StringBuffer();
		try (FileReader reader = new FileReader(filePath); BufferedReader br = new BufferedReader(reader) // 建立一个对象，它把文件内容转成计算机能读懂的语言
		) {
			String line;
			// 网友推荐更加简洁的写法
			while ((line = br.readLine()) != null) {
				// 一次读入一行数据
				sb.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public String writeFile(String content, String url) {
		if (StringUtils.isEmpty(url)) {
			String fileNewName = UUID.randomUUID().toString() + ".html";
			SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
			String time = sf.format(new Date());
			String saveFullPath = savePath + time;
			File targetFile = new File(saveFullPath);
			// 判断目录是否存在 不存在：创建目录
			if (!targetFile.exists()) {
				targetFile.mkdirs();
			}
			writeHtml(saveFullPath+"/"+fileNewName, content);
			return  reqPath + time + "/" + fileNewName;
		}else {
			String filePath=url.replace(reqPath, savePath);
			writeHtml(filePath, content);
			return url;
		}
	}

	private void writeHtml(String fielPath, String content) {
		try {
			File writeName = new File(fielPath);
			if (!writeName.exists()) {
				writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
			}
			try (FileWriter writer = new FileWriter(writeName); BufferedWriter out = new BufferedWriter(writer)) {
				out.write(content); // \r\n即为换行
				out.flush(); // 把缓存区内容压入文件
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}